package com.huse.pigservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huse.commonutls.ApiResult;
import com.huse.pigservice.entity.Pig;
import com.huse.pigservice.entity.PigType;
import com.huse.pigservice.service.PigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.Get;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xinxin
 * @since 2021-05-26
 */
@RestController
@Api(tags = "pig")
@RequestMapping("/pigservice/pig")
@CrossOrigin("*")
public class PigController {
    @Autowired
    private PigService pigService;

    @PostMapping("/")
    @ApiOperation("猪的信息录入")
    public ApiResult registerPig(Pig pig){
        pig.setBirthday(pig.getBirthday());
        pigService.save(pig);
        return ApiResult.ok().message("猪信息录入成功！");
    }

    @GetMapping("/")
    @ApiOperation("猪的信息分页查询")
    public ApiResult getPigInfo(int start,int size){
        Page<Pig> page = new Page<>(start,size);
        QueryWrapper<Pig> wapper = new QueryWrapper();
//        排除两个字段
        wapper.select(Pig.class, info -> !info.getColumn().equals("create_time")
                && !info.getColumn().equals("update_time"));

        Page<Pig> p = pigService.getPage(page, wapper);
        return ApiResult.ok().data("pigList",p.getRecords());
    }

    @GetMapping("/pigType")
    @ApiOperation("获取所有的猪类型")
    public ApiResult getPigType(){
        return ApiResult.ok().data("pigType",pigService.getPigTypeList());
    }


    @GetMapping("pigTree")
    public ApiResult getPigTree(){
        List<Pig> list = pigService.getPigTree();
        return ApiResult.ok().data("pigTree",list);
    }
}

